unit Filter;

interface

uses
  GlobalRecordsAndEnums,
  Classes;

type
  SItemIDList = array of Integer;

  TFilter = class
    private
      _name: String;
      _items: array of Integer;
    public
      constructor Create;
      destructor Destroy;

      procedure AddItem(itemID: Integer);
      procedure AddItems(itemIDs: SItemIDList);
      function IsInFilter(itemID: Integer): Boolean;

      property Name: String read _name;
  end;

  TFilterList = class
    private
      _filters: TList;
    public
      constructor Create;
      destructor Destroy;

      function AddFilter(filter: TFilter): Integer;
      function GetFilter(index: Integer): TFilter;
      procedure DeleteFilter(filter: TFilter);
  end;

implementation

//=====================
//====== TFilter ======
//=====================

constructor TFilter.Create;
begin
  inherited Create;
end;

destructor TFilter.Destroy;
begin
  inherited Destroy;
end;

procedure TFilter.AddItem(itemID: Integer);
var
  len: Integer;
begin
  len := Length(_items);
  SetLength(_items, len +1);
  _items[len] := itemID;
end;

procedure TFilter.AddItems(itemIDs: SItemIDList);
var
  i, len: Integer;
begin
  for i := 0 to Length(itemIDs) -1 do begin
    len := Length(_items);
    SetLength(_items, len +1);
    _items[len] := itemIDs[i];
  end;
end;

function TFilter.IsInFilter(itemID: Integer): Boolean;
var
  i: Integer;
begin
  result := false;
  for i := 0 to Length(_items) -1 do begin
    if (_items[i] = itemID) then begin
      result := true;
      break;
    end;
  end;
end;

//=========================
//====== TFilterList ======
//=========================

constructor TFilterList.Create;
begin
  inherited Create;
  _filters := TList.Create;
end;

destructor TFilterList.Destroy;
var
  i: Integer;
begin
  for i := 0 to _filters.Count -1 do begin
    TFilter(_filters[i]).Destroy;
  end;
  _filters.Destroy;
end;

function TFilterList.AddFilter(filter: TFilter): Integer;
begin
  if (filter <> nil) then _filters.Add(filter);
end;

function TFilterList.GetFilter(index: Integer): TFilter;
begin
  if (index > _filters.Count -1) then result := nil
  else result := TFilter(_filters[index]);
end;

procedure TFilterList.DeleteFilter(filter: TFilter);
begin
  // do stuff
end;

end.

